<template>
  <div>
    <!--    搜索用户信息-->
    <div id="search" style="text-align: right">
      <el-form :inline="true" id="demo-form-inline">
        <el-form-item label="用户名" style="margin-right: 100px">
          <el-input @keyup.enter.native="handleSearch" v-model="formDate.username"
                    placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="用户描述">
          <el-input @keyup.enter.native="handleSearch" v-model="formDate.descp" placeholder="请输入角色描述"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 100px;margin-right: 150px;">
          <el-button style="width: 150px;" type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider></el-divider>
    <!--    展示用户信息-->
    <div id="list">
      <el-table :data="tableData" style="width: 100%" :stripe="true" :border="true"
                :highlight-current-row="true" height="450">
        <el-table-column label="用户编号" width="150px" prop="id"></el-table-column>
        <el-table-column label="用户名" width="150px" prop="username"></el-table-column>
        <el-table-column type="password" label="密码" width="150px" prop="password"></el-table-column>
        <el-table-column label="用户所属角色" prop="roleName">
          <template v-slot="scope">
            <el-tag
              :type="scope.row.roleName==='ROLE_super_admin'? 'danger' : (scope.row.roleName==='ROLE_boss')?'warning':'info'"
              disable-transitions>{{ scope.row.roleName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="scope">
            <el-button size="mini" @click="change(scope.row.id)">修改</el-button>
            <el-button size="mini" type="danger" @click="open(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    修改用户信息-->
    <el-dialog title="修改用户信息" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="用户编号" :label-width="formLabelWidth">
          <el-input placeholder="请输入内容" v-model="user.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input placeholder="请输入用户名" v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" :label-width="formLabelWidth">
          <el-input :disabled="true" type="password" v-model="user.password" placeholder="请输入权限描述"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户所属角色">
          <el-select v-model="user.roleId" clearable placeholder="请选择用户所属角色">
            <el-option
              v-for="item in RoleList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--    分页-->
    <div id="block">
      <el-pagination background
                     :current-page.sync="pagination.page"
                     :page-size="pagination.pageSize"
                     :total="pagination.total"
                     :page-sizes="[7,15,30]"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     layout="total, prev, pager, next, sizes">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import roleApi from "@/api/role";
import userApi from "@/api/user";
import qs from "qs";

export default {
  name: "list",
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      formDate: {
        username: '',
        descp: "",
      },
      pagination: {
        total: 0,
        page: 1,
        pageSize: 7
      },
      timeout: null,
      user: {
        id: "",
        username: "",
        password: "",
        roleId: ''
      },
      RoleList: []
    }
  },
  mounted() {
    this.queryUser()
  },
  methods: {
    //页码改变时的回调函数
    handleCurrentChange(page) {
      this.pagination.page = page;
      this.queryRole();
    },
    //页面大小改变时的回调函数
    handleSizeChange(pageSize) {
      this.pagination.pageSize = pageSize;
      this.queryUser();
    },
    //打开确认删除的提示框
    open(id) {
      this.$confirm('此操作删除该用户, 是否继续?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(id);
      })
    },
    //点击修改按钮
    change(id) {
      this.selectById(id)
      this.selectAllRole()
      this.dialogFormVisible = true;
    },
    //修改用户信息函数
    handleEdit() {
      const updateMSG = {
        id: this.user.id,
        username: this.user.username,
        password: this.user.password,
        roleId:this.user.roleId
      }
      userApi.updateUser(qs.stringify(updateMSG, {indices: false})).then(resp => {
        if (resp.code === 6666) {
          this.$message.success("修改成功！")
          this.queryUser()
          this.dialogFormVisible = false;
        } else {
          this.$message.info(resp.msg)
        }
      }).catch(() => {
        this.$message.error("修改失败！")
      });
    },
    //删除用户信息函数
    handleDelete(id) {
      userApi.deleteUser(id).then(resp => {
        if (resp.code === 6666) {
          this.$message.success('删除成功!');
          this.queryUser();
        } else {
          this.$message.info('删除失败')
        }
      }).catch(error => {
        this.$message.error('删除出错');
      })
    },
    //点击查询时回到第一页
    handleSearch() {
      this.pagination.page = 1;
      this.queryUser()
    },
    //分页查询用户链表
    queryUser() {
      const pagination = {
        username: this.formDate.username,//搜索框中的值
        descp: this.formDate.descp,
        page: this.pagination.page,//当前页码
        pageSize: this.pagination.pageSize,//每页多少条数据
      }
      userApi.selectUserList(pagination).then(resp => {
        if (resp.code === 6666) {
          this.tableData = resp.data;
          this.pagination.page = resp.pageInfo.page;
          this.pagination.pageSize = resp.pageInfo.pageSize;
          this.pagination.total = resp.pageInfo.total;
        } else {
          this.$message.info('获取用户列表失败')
        }
      }).catch(() => {
        this.$message.error("查询用户列表出错！")
      })
    },
    //取消修改的提示信息
    handleCancle() {
      this.dialogFormVisible = false;
    },
    //根据id查询用户信息
    selectById(id) {
      userApi.selectUserById(id).then(resp => {
        if (resp.code === 6666) {
          this.user.id = resp.data.id
          this.user.username = resp.data.username
          this.user.password = resp.data.password
          this.user.roleId = resp.data.roleId
        } else {
          this.$message.warning('查询用户信息失败')
        }
      }).catch(() => {
        this.$message.error('查询用户信息出错')
      })
    },
    //查询角色列表
    selectAllRole(){
      roleApi.selectAllRole().then(resp=>{
        if (resp.code===6666){
          this.RoleList=resp.data
        }else {
          this.$message.warning('查询角色信息失败')
        }
      }).catch(()=>{
        this.$message.error('查询角色信息出错')
      })
    }
  },
}
</script>
<style scoped>

#list {
  margin-left: 20px;
  margin-right: 20px;
}

#search {
  margin-left: 100px;
  margin-top: 20px;
}

#block {
  margin-top: 20px;
  margin-left: 20px;
}

#demo-form-inline {
  margin-bottom: 0;
}

.el-divider {
  margin: 10px 0;
}
</style>
